package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.*;
import org.example.system.mapper.AttendanceMapper;
import org.example.system.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 考勤服务实现
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;

    /**
     * 按条件分页查询
     *
     * @param attendance
     * @return
     */

    @Override
    public IPage<AttendanceEntity> selectPage(AttendanceEntity attendance) {
        MPJLambdaWrapper<AttendanceEntity> wrapper = new MPJLambdaWrapper<>();
        // 条件
        if (attendance.getStudentId() != null) {
            wrapper.eq(AttendanceEntity::getStudentId, attendance.getStudentId());    // 相同的学生ID（精确查询）
        }
        if (attendance.getCourseId() != null) {
            wrapper.eq(AttendanceEntity::getCourseId, attendance.getCourseId());    // 相同的课程ID（精确查询）
        }
        if (attendance.getTeacherId() != null) {
            wrapper.eq(AttendanceEntity::getTeacherId, attendance.getTeacherId());    // 相同的老师ID（精确查询）
        }
        if (StrUtil.isNotEmpty(attendance.getCourseName())) {
            wrapper.like(CourseEntity::getName, attendance.getCourseName());
        }
        if (StrUtil.isNotEmpty(attendance.getTeacherName())) {
            wrapper.like(TeacherEntity::getUsername, attendance.getTeacherName());
        }
        if (StrUtil.isNotEmpty(attendance.getStudentName())) {
            wrapper.like(StudentEntity::getUsername, attendance.getStudentName());
        }
        if (StrUtil.isNotEmpty(attendance.getStudentMajor())) {
            wrapper.like(StudentEntity::getMajor, attendance.getStudentMajor());
        }
        if (StrUtil.isNotEmpty(attendance.getStatus())) {
            wrapper.like(AttendanceEntity::getStatus, attendance.getStatus());
        }
        //联查CourseEntity、TeacherEntity、StudentEntity
        wrapper.selectAll(AttendanceEntity.class);
        wrapper.leftJoin(CourseEntity.class, CourseEntity::getId, AttendanceEntity::getCourseId);
        wrapper.leftJoin(TeacherEntity.class, TeacherEntity::getId, AttendanceEntity::getTeacherId);
        wrapper.leftJoin(StudentEntity.class, StudentEntity::getId, AttendanceEntity::getStudentId);
        wrapper.selectAs(CourseEntity::getName, "courseName");
        wrapper.selectAs(TeacherEntity::getNumber, "teacherNum");
        wrapper.selectAs(TeacherEntity::getUsername, "teacherName");
        wrapper.selectAs(StudentEntity::getNumber, "studentNum");
        wrapper.selectAs(StudentEntity::getUsername, "studentName");
        wrapper.selectAs(StudentEntity::getMajor, "studentMajor");

        IPage<AttendanceEntity> page = new Page<>();
        page.setCurrent(attendance.getPageNum());
        page.setSize(attendance.getPageSize());
        return attendanceMapper.selectPage(page, wrapper);
    }


    /**
     * 添加排课
     *
     * @param attendance
     * @return
     */
    @Override
    public int add(AttendanceEntity attendance) {
        attendance.setATime(new Date());
        return attendanceMapper.insert(attendance);
    }
}
